define([ 'require', 'game/types/hex', 'game/math', 'game/canvas', 'game/consts' ], function(require, hextypes, math, canvas, consts) {
    return function(grid_x, grid_y, type) {
        var hex = {

            grid_x: grid_x,
            grid_y: grid_y,
            type: type,
            selected: false,

            get_center_xy: function() {
                if (!this.center) {
                    this.center = math.axial_to_center_xy({
                        x: grid_x,
                        y: grid_y
                    });
                }
                return this.center;
            },

            draw: function() {
                if (typeof type === 'undefined')
                    type = 'default';
                var hextype = hextypes[type] || hextypes['unknown'];
                var canvas = require('game/canvas');
                var ctx = canvas.ctx;
                var center = this.get_center_xy();
                ctx.strokeStyle = hextype.stroke;
                ctx.fillStyle = hextype.fill;
                ctx.beginPath();
                for (var i = 0; i < 6; ++i) {
                    var c = math.corner_xy(center, i);
                    if (i == 0) {
                        ctx.moveTo(c.x, c.y);
                    } else {
                        ctx.lineTo(c.x, c.y);
                    }
                }
                ctx.closePath();
                ctx.stroke();
                ctx.fill();
                this.redraw_inner();
            },

            redraw_inner: function(selected) {
                var canvas = require('game/canvas');
                var ctx = canvas.ctx;
                var center = this.get_center_xy();
                ctx.strokeStyle = '#ffffff';
                ctx.fillStyle = '#ffffff';
                ctx.beginPath();
                for (var i = 0; i < 6; ++i) {
                    var c = math.corner_xy(center, i, 0.9);
                    if (i == 0) {
                        ctx.moveTo(c.x, c.y);
                    } else {
                        ctx.lineTo(c.x, c.y);
                    }
                }
                ctx.closePath();
                ctx.stroke();
                ctx.fill();
                if (this.selected) {
                    ctx.fillStyle = '#e0ffff';
                    ctx.beginPath();
                    for (var i = 0; i < 6; ++i) {
                        var c = math.corner_xy(center, i, 0.8);
                        if (i == 0) {
                            ctx.moveTo(c.x, c.y);
                        } else {
                            ctx.lineTo(c.x, c.y);
                        }
                    }
                    ctx.closePath();
                    ctx.stroke();
                    ctx.fill();
                }
                var hextype = hextypes[type] || hextypes['unknown'];
                if (hextype.glyph) {
                    ctx.textAlign = 'center';
                    ctx.textBaseline = 'middle';
                    ctx.font = /* 'bold ' */+(3 / 4 * consts.el) + 'px ' + (hextype.font ? hextype.font : 'Helvetica');
                    ctx.fillStyle = hextype.stroke;
                    ctx.fillText(hextype.glyph, center.x, center.y);
                }
            },

            onclick: function() {

            }

        };
        return hex;
    };
});
